<template>
  <!-- 书写html结构 只可以有一个div 但是可以嵌套多个div -->
  <div>
    <!-- 根据参数查询 和 添加数据 -->
    <el-row>
      <el-col :span="4">
        <el-input
          v-model="searchForm.loginName"
          placeholder="请输入门店名称"></el-input>
      </el-col>
      <el-col :span="6">
        <el-button
          type="success"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          >添加</el-button
        >
      </el-col>
      <el-col :span="6"></el-col>
    </el-row>
    <!-- 查询的数据指定数据 -->
    <el-table :data="tableData" highlight-current-row style="width: 100%">
      <!-- <el-table-column
        sortable
        width="150"
        prop="evaluateId"
        label="Id">
      </el-table-column> -->
      <el-table-column
        sortable
        width="180"
        prop="storeId"
        :formatter="getStatus2"
        label="外键关联门店表">
      </el-table-column>
      <el-table-column
        sortable
        width="180"
        prop="userId"
        :formatter="getStatus3"
        label="评价人关联用户表">
      </el-table-column>
      <el-table-column
        sortable
        width="100"
        prop="content"
        :formatter="getStatus4"
        label="评价内容">
      </el-table-column>
      <el-table-column
        sortable
        width="110"
        prop="evaluateTime"
        label="评价时间">
      </el-table-column>
      <!-- <el-table-column
        sortable
        width="150"
        prop="picture"
        label="图片">
      </el-table-column> -->
      <el-table-column label="门店图片" width="160" height="120">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.picture"></el-image>
        </template>
      </el-table-column>
        <el-table-column
        sortable
        width="150"
        prop="star"
        label="星级">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteDept(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="success"
            @click="refresh()"
            >刷新缓存</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 6, 12, 24, 120]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!--    添加修改弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          label="外键关联门店表"
          :label-width="formLabelWidth"
          prop="storeId">
          <el-input v-model="form.storeId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="评价人关联用户表"
          :label-width="formLabelWidth"
          prop="userId">
          <el-input v-model="form.userId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="评价内容"
          :label-width="formLabelWidth"
          prop="content">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="评价时间"
          :label-width="formLabelWidth"
          prop="evaluateTime">
          <el-input v-model="form.evaluateTime" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="评论图片" prop="url" :label-width="formLabelWidth">
          <!--上传图片  -->
          <el-upload
            style="height: 180px;width: 180px;"
            class="avatar-uploader"
            action="http://172.16.9.55:16868/upImage/uploadFile"
            name="file1"
            :show-file-list="false"
            :on-success="handleFileSuccess"
            :before-upload="beforeFileUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item
          label="星级"
          :label-width="formLabelWidth"
          prop="star">
          <el-input v-model="form.star" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!--授予角色表单-->
    <el-dialog title="角色列表" :visible.sync="dialogVisible" width="30%">
      <el-checkbox
        :indeterminate="isIndeterminate"
        v-model="checkAll"
        @change="handleCheckAllChange"
        >全选</el-checkbox
      >
      <div style="margin: 15px 0"></div>
      <el-checkbox-group
        v-model="checkedRoles"
        @change="handleCheckedRolesChange">
        <el-checkbox
          v-for="role in roleList" v-if="role.roleId==1"
          :label="role.roleId"
          :key="role.roleId"
         disabled >{{ role.roleName }}
        </el-checkbox>
           <el-checkbox
          v-for="role in roleList" v-if="role.roleId!=1"
          :label="role.roleId"
          :key="role.roleId"
          >{{ role.roleName }}
        </el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserAndRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<!--书写组件配置项目  相当于写js代码的地方 -->
<script>
//数据 全是json格式
export default {
  //注册属性 父传子 数据传递
  props: {},
  data() {
    //定义数据
    return {
      tableData: [], //表格数据
      currentPage: 1, //页码
      pageSize: 3, //每页显示的数量
      total: 100, //分页总数量
      searchForm: {storeId: "", userId: ""}, //查询携带的参数
      dialogFormVisible: false, //使弹出不显示
      title: "评论添加", //title 默认为 用户添加
      //添加或者更新数据时 携带的参数
      form: {},
      //弹出框的宽度
      formLabelWidth: "120px",
      isIndeterminate: true,
      tempUserId: 0, //用户id
      //结束
      imageUrl: "", //上传照片设置
    };
  },
  //初始化数据
  created() {
    this.loadData();
  },
  //方法书写
  methods: {
    //定义变量_this等于外面的this
    //封装请求数据的方法
    loadData() {
      var _this = this;
      this.$http
        .post("/tbStoreEvaluate/queryPage", {
          pageNo: this.currentPage,
          pageSize: this.pageSize,
          data:this.searchForm
        })
        .then(function (response) {
          console.log(response);
          //判断是否执行成功
          if (response.data.code == 200) {
            _this.tableData = response.data.data.records;
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //实行每页有多少条的方法
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.loadData();
    },
    //实现跳转页的方法
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.loadData();
    },
    //刷新
    refresh() {
      //原始js重新请求
      location.reload();
    },
    //查询方法
    search() {
      this.loadData();
      //搜索时,使页码为1
      this.currentPage = 1;
    },
    //用户更新的方法
    handleEdit(index, row) {
      var _this = this;
      console.log(index, row);
      //使弹出框显示
      this.dialogFormVisible = true;
      //给用户赋值
      this.title = "评论修改";
      //点击查询携带数据
      this.form = row;
      //照片方法
      this.imageUrl = row.avatar;
    },
    //添加数据方法
    handleAdd() {
      //使弹出框显示
      this.dialogFormVisible = true;
      //给用户赋值
      this.title = "评论添加";
      //点击查询携带数据
      this.form = {};
      //照片方法
      this.imageUrl = "";
    },
    //关闭方法
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //删除方法
    deleteDept(row) {
      //确认框提示
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var _this = this;
          this.$http
            .delete("/tbStoreEvaluate/deleteById?id=" + row.userId)
            .then(function (response) {
              console.log(response);
              if (response.data.code == 200) {
                _this.$message({
                  message: "操作成功",
                  type: "success",
                });
                _this.dialogFormVisible = false;
                _this.loadData();
              } else {
                _this.$message("操作失败");
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //实现用户添加或者更新
    addOrUpdate() {
      var uri = "/tbStoreEvaluate/add";
      if (this.title == "评论修改") {
        uri = "/tbStoreEvaluate/update";
      }
      var _this = this;
      this.$http
        .post(uri, this.form)
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            _this.$message({
              message: "操作成功",
              type: "success",
            });
            _this.dialogFormVisible = false;
            _this.loadData();
          } else {
            _this.$message("操作失败");
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //处理文件成功后的处理方法
    handleFileSuccess(res, file) {
      //回显上传图片
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.picture = file.response.data.filePath;
    },
    //处理文件成功前的处理方法
    beforeFileUpload(file) {
      const FILE_NAME = file.name;
      if (
        FILE_NAME.substring(FILE_NAME.lastIndexOf(".")) !== ".bmp" &&
        FILE_NAME.substring(FILE_NAME.lastIndexOf(".")) !== ".jpg" &&
        FILE_NAME.substring(FILE_NAME.lastIndexOf(".")) !== ".png"
      ) {
        this.$message.warning("仅支持.bmp.jpg.png文件");
        return false;
      }
      // const isJPG = file.type === 'image/jpeg';
      const isLt6M = file.size / 1024 / 1024 < 6;
      // if (!isJPG) {
      //     this.$message.error('上传头像图片只能是 JPG 格式!');
      // }
      if (isLt6M) {
        this.file = file;
        return true;
      }
      this.$message.warning("请上传不超过6M的文件.");
      return false;
    },
  },
  //声明注解
  components: {},
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
